

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.plaf.FontUIResource; 

/**
 * The TicketGrid creates the container that will hold orders for individual seats on the 
 * Server screen.  This has been designed as to allow the server to enter the number of customers
 * at a table (though that method needs to be written).  Additional testing needs to be done to 
 * determine how this looks within multiple panes rather than a stand-alone screen. 
 * @author Mike Jones
 * @version 1.0
 */
public class TicketGrid extends JPanel {

	private JPanel mainPanel;
	private JPanel panel;
	private JPanel numbersPanel;
	private JTextArea order;
	private JTextField amount;
	private JButton header;
	private JScrollPane scroll;
	private static ArrayList<JTextArea> arrayTA;
	private static ArrayList<JButton> arrayJB;
	private static int seatNum;
	private static boolean isHeaderClicked = false;

	private Toolkit tk = Toolkit.getDefaultToolkit();


	/**
	 * Holds the values for the maximum height and width for the user screen size.
	 */
	private int screenWidth = ((int) tk.getScreenSize().getWidth());
	private int screenHeight = ((int) (tk.getScreenSize().getHeight()));


	/**
	 * Builds the ticket displayer
	 */
	public TicketGrid(int customers){
		this.setLayout(new BorderLayout());
		mainPanel = new JPanel();
		mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.X_AXIS));
		//this call needs to be an action listener from the square table
			makePanel(customers);
	}//End of Constructor



	/**
	 * Makes as many ticket panels as there are customers - scrollbar is set up so that
	 * only 3 tickets will be displayed at one time
	 * @param c - the number of customers
	 */
	private void makePanel(int c){

		arrayJB = new ArrayList<JButton>();
		arrayTA = new ArrayList<JTextArea>();

		for(int i = 1; i <= c; i++){
			panel = new JPanel(new BorderLayout());
			panel.setBorder(BorderFactory.createLineBorder(Color.white, 5));
			header = new JButton("Seat "+i);
			header.setFont(new Font("Arial", Font.PLAIN, 40));
			header.setBackground(new Color(91,155,213));
			header.setForeground(Color.white);

			header.addActionListener(new ActionListener(){
				public void actionPerformed(ActionEvent e){
					String buttonLabel = e.getActionCommand();
					seatNum = Integer.parseInt(buttonLabel.substring(buttonLabel.indexOf(" ")+1));
					setIsHeaderClicked(seatNum);
					setSeatNumber(seatNum);
				}
			});

			panel.add(header, BorderLayout.NORTH);
			order = new JTextArea();
			order.setEditable(false);
			order.setLineWrap(true);
			order.setWrapStyleWord(true);
			order.setBackground(new Color(210,222,239));
			order.setPreferredSize(order.getPreferredSize());
			order.setFont(new Font("Arial", Font.PLAIN, 25));
			panel.setPreferredSize(new Dimension(screenWidth/4, screenHeight));
			panel.add(order);
			mainPanel.add(panel);

			arrayJB.add(header);//Used to keep location of a certain JButton header (always minus one)
			arrayTA.add(order);//Used to keep location of a certain JTextArea (always minus one)
		}
		//if more than 3 customers at a table display the scroll bar
		if (c > 3) {
			scroll = new JScrollPane(mainPanel);
			scroll.setHorizontalScrollBarPolicy(scroll.HORIZONTAL_SCROLLBAR_ALWAYS);
			scroll.getVerticalScrollBar().setPreferredSize(new Dimension(screenWidth/25, 0));
			scroll.getHorizontalScrollBar().setPreferredSize(new Dimension(0, screenHeight/20));
			this.add(scroll, BorderLayout.CENTER);
		}
		else{
			this.add(mainPanel);
		}

	}//End of makePanel method

	/**
	 * Sets the boolean isHeaderClicked to ensure only one seat number button is highlighted at a time to add to orders
	 * Clicking on a already highlighted button will unhighlight the button 
	 * @param n - the seat number in the array of seat number buttons
	 */
	private static void setIsHeaderClicked(int n){

		if(isHeaderClicked == false && arrayJB.get(n-1).getForeground() == Color.WHITE){
			arrayJB.get(n-1).setBackground(Color.YELLOW);
			arrayJB.get(n-1).setForeground(Color.BLACK);
			isHeaderClicked = true;
		}
		else{
			arrayJB.get(n-1).setBackground(new Color(91,155,213));
			arrayJB.get(n-1).setForeground(Color.white);
			isHeaderClicked = false;
		}
	}//End of setIsHeaderClicked

	/**
	 * Returns the boolean value of getIsHeaderClicked
	 * @return isHeaderClicked - true or false as to whether a seat number button is clicked
	 */
	public static boolean getIsHeaderClicked(){
		return isHeaderClicked;
	}//End getIsHeaderClicked method

	/**
	 * Sets the integer that contains the seat number button that was selected
	 * @param n - the seat number button in ArrayJB
	 */
	private static void setSeatNumber(int n){
		seatNum = n-1;
	}//end setSeatNumber

	/**
	 * Gets the integer that contains the seat number button that was selected
	 * @return seatNum - the seat number button in ArrayJB
	 */
	public static int getSeatNumber(){
		return seatNum;
	}//end getSeatNumber

	/**
	 * Sets the JTextArea that contains the ticket order.  "Price" is a placeholder for the actual price.
	 * @param i - the item to be added to the Order JTextArea
	 */
	public static void setTicketOrder(String i){
		if(arrayTA.get(seatNum).getText().equals("")){
			arrayTA.get(seatNum).setText(i + "\t" + "Price" + "\n" + "\n" + "Total:" + "\t" + "Total Amount");
		}
		else {
			String t = arrayTA.get(seatNum).getText().replaceAll("\n" + "\n" + "Total:" + "\t" + "Total Amount", "");
			arrayTA.get(seatNum).setText(t + "\n" + i + "\t" + "Price" + "\n" + "\n" + "Total:" + "\t" + "Total Amount");
		}
		arrayTA.get(seatNum).setFont(new Font("Arial", Font.PLAIN, 30));
	}// End getSeatNumber


}//End of Class
